package com.woniuxy.company5.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.company5.dto.FinMouthBillDto;
import com.woniuxy.company5.entity.FinMouthBill;
import com.woniuxy.company5.service.FinMouthBillService;
import com.woniuxy.company5.util.Result;
import com.woniuxy.company5.vo.FinMouthBillVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 功能描述
 *
 * @author PC
 * @date -2022/12/7-时间-17:10
 * @product company5
 */
@RestController
@RequestMapping("/FinMouthBills")
public class FinMouthBillController {
    @Resource
    private FinMouthBillService finMouthBillService;

    @PostMapping("selAll")
    public Result selAll(@RequestBody FinMouthBillDto finMouthBillDto) {
        PageHelper.startPage(finMouthBillDto.getCurrentPage(), finMouthBillDto.getPageSize(), true);
        PageHelper.orderBy("id desc");
        List<FinMouthBillVo> finMouthBillVos = finMouthBillService.selAll(finMouthBillDto);
        PageInfo<FinMouthBillVo> pageInfo = new PageInfo<>(finMouthBillVos);
        return Result.ok().data("billList",finMouthBillVos).data("total",pageInfo.getTotal());
    }


    @PostMapping("")
    public Result add(@RequestBody FinMouthBill finMouthBill){
        int i = finMouthBillService.insertSelective(finMouthBill);
        if(i > 0){
            return Result.ok();
        }else{
            return Result.error();
        }

    }
    @GetMapping("{id}")
    public Result selOne(@PathVariable Long id){
        FinMouthBill finMouthBill = finMouthBillService.selectByPrimaryKey(id);
        if(finMouthBill != null){
            return Result.ok().data("list",finMouthBill);
        }else {
            return Result.error();
        }
    }

    /**
     * 年度流水图表
     * @return
     */
    @GetMapping("")
    public Result Histogram(){
        List<BigDecimal> PublicAccounts = finMouthBillService.PublicAccounts();
        List<BigDecimal> totalRevenue = finMouthBillService.TotalRevenue();
        List<String> totalExpenditure = finMouthBillService.TotalExpenditure();
        return Result.ok().data("GeneralLedgerList",PublicAccounts).data("TotalRevenueList",totalRevenue).data("TotalExpenditureList",totalExpenditure);
    }
    @PutMapping("")
    public Result update(@RequestBody FinMouthBill finMouthBill){
        int i = finMouthBillService.updateByPrimaryKeySelective(finMouthBill);
        if(i > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @DeleteMapping("{id}")
    public Result del(@PathVariable Long id){
        int i = finMouthBillService.deleteByPrimaryKey(id);
        if(i > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

}
